package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
public class Usuario extends EntityManaged implements Serializable, UserDetails {

	private static final long serialVersionUID = 1082824403447060113L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private String username;

	private String password;

	private Date dataUltimoAcesso;

	private Date dataAcessoAtual;

	@Transient
	private Collection<GrantedAuthority> authorities;

	@OneToOne
	private Pessoa pessoa;

	@ManyToMany(fetch = FetchType.EAGER)
	private List<PapelUsuario> listaPapeisUsuario;

	public Usuario() {
		listaPapeisUsuario = new ArrayList<PapelUsuario>();
	}

	public void addPapelUsuario(PapelUsuario papelUsuario) {
		listaPapeisUsuario.add(papelUsuario);
	}

	public void removerPapelUsuario(PapelUsuario papelUsuario) {
		listaPapeisUsuario.remove(papelUsuario);
	}

	@Override
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<PapelUsuario> getListaPapeisUsuario() {
		return listaPapeisUsuario;
	}

	public void setListaPapeisUsuario(List<PapelUsuario> listaPapeisUsuario) {
		this.listaPapeisUsuario = listaPapeisUsuario;
	}

	public Date getDataUltimoAcesso() {
		return dataUltimoAcesso;
	}

	public void setDataUltimoAcesso(Date dataUltimoAcesso) {
		this.dataUltimoAcesso = dataUltimoAcesso;
	}

	public Date getDataAcessoAtual() {
		return dataAcessoAtual;
	}

	public void setDataAcessoAtual(Date dataAcessoAtual) {
		this.dataAcessoAtual = dataAcessoAtual;
	}

	public Pessoa getPessoa() {
		return pessoa;
	}

	public void setPessoa(Pessoa pessoa) {
		this.pessoa = pessoa;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return isAtivo();
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

}
